package com.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.gulimall.product.entity.ProductAttrValueEntity;
import com.gulimall.product.service.ProductAttrValueService;
import com.gulimall.product.vo.AttrResVO;
import com.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gulimall.product.entity.AttrEntity;
import com.gulimall.product.service.AttrService;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.R;



/**
 * 商品属性
 *
 * @author kipp
 * @email aaa@gmail.com
 * @date 2021-11-21 01:37:10
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    @RequestMapping("/base/listforspu/{spuId}")
    public R spuList(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> entityList = productAttrValueService.baseAttrlistforspu(spuId);
        return R.ok().put("data", entityList);
    }

    @RequestMapping("/update/{spuId}")
    public R update(@RequestBody List<ProductAttrValueEntity> entities,@PathVariable("spuId") Long spuId){
        productAttrValueService.updatespuAttr(spuId,entities);

        return R.ok();
    }

    @RequestMapping("/info/{attrId}")
    public R attrInfo(@PathVariable("attrId") Long attrId){
        AttrResVO resVO = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", resVO);
    }

//    @RequestMapping("/base/list/{catelogId}")
    @RequestMapping("/{attrType}/list/{catelogId}")
    public R baseList(@RequestParam Map<String, Object> params,
                      @PathVariable("catelogId") Long catelogId,
                      @PathVariable("attrType") String attrType){

        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,attrType);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
//    @RequestMapping("/info/{attrId}")
//    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
//
//        return R.ok().put("attr", attr);
//    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    public R save(@RequestBody AttrEntity attr){
    public R save(@RequestBody AttrVo attr){
//		attrService.save(attr);
		attrService.saveAttrVo(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
